#include <iostream>
using namespace std;

int find(int **fa, int i){
    if((*fa)[i] == i){
        return i;
    }
    (*fa)[i] = find(fa, (*fa)[i]);
    return (*fa)[i];
}

void merge(int ** fa, int a, int b){
    (*fa)[find(fa, a)] = find(fa, b);
}

typedef struct node{
    int x, y;
}Two;

int main(){
    int n, a, b, rel;
    scanf("%d", &n);
    //父节点数组
    int fa[n + 1];
    //初始化
    for(int i = 0; i < n + 1; i++){
        fa[i] = i;
    }
    //存储矛盾双方
    Two two[n];
    int cur = 0;
    for(int i = 0; i < n; i++){
        scanf("%d%d%d", &a, &b, &rel);
        if(rel == 1){//合并
            int *p = fa;
            merge(&p, a, b);
        }else{//记录
            two[cur++] = {a, b};
        }
    }
    bool res = true;
    for(int i = 0; i < cur; i++){
        Two t = two[i];
        int *p = fa;
        if(find(&p, t.x) == find(&p, t.y)){
             res = false;
             break;
        }
    }
    if(res){
        printf("YES");
    }else{
        printf("NO");
    }
    return 0;
}